#include <iostream>
#include <string.h>
using std::cout;
using std::endl;

class CharArray
{
public:
    CharArray(const char* pstr)
    :_capacity(strlen(pstr)+1)
    ,_data(new char[_capacity]())
    {
        strcpy(_data,pstr);
    }

    ~CharArray(){
        if(_data){
            delete [] _data;
            _data = nullptr;
        }
    }
    char & operator[](size_t idx){
        if(idx < _capacity - 1){
            return _data[idx];
        }
        else{
            cout << "out of range!" << endl;
            static char nullchar = '\0';
            return nullchar;
        }
    }

    char & operator[](size_t idx)const{
        if(idx < _capacity - 1){
            return _data[idx];
        }
        else{
            cout << "out of range!" << endl;
            static char nullchar = '\0';
            return nullchar;
        }
    }
private:
    size_t _capacity;
    char* _data;
};


void test0(){
    CharArray ca("xixihaha");
    cout << ca[0] << endl;
    ca[0] = 'p';
    cout << ca[0] << endl;
}

int main(int argc, char * argv[])
{
    test0();
    return 0;
}


